<template>
  <div >

    <tableComponents
      :pagination="{currentPage,pageSize,total:list.all_num}"
      @handleCurrentChange="handleCurrentChange"
      @handleSizeChange="handleSizeChange"
      @sendUserSearch="getSearch"
      :searchData="userSearchData"
      :isHaveExport="false"
      @toAdd="myshow=true"
      addText='红包发放'
    >
      <template v-slot:table>
        <el-table
          ref="multipleTable"
          :data="list.data"
          stripe
          v-loading="loading"
          header-row-class-name="head_color"
          tooltip-effect="dark"
          style="width: 99.9%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" class="tc" width="35"></el-table-column>

          <el-table-column label="券号" min-width="80">
            <template slot-scope="scope">{{ scope.row.coupon_num }}</template>
          </el-table-column>

          <el-table-column label="领取用户" min-width="80">
            <template slot-scope="scope">{{ scope.row.username }}</template>
          </el-table-column>

          <el-table-column label="额度" min-width="50">
            <template slot-scope="scope">{{ scope.row.money|keepTwoNum }}</template>
          </el-table-column>

          <el-table-column label="消费满" min-width="50">
            <template slot-scope="scope">{{ scope.row.xfm }}</template>
          </el-table-column>

          <el-table-column label="所属商家" min-width="50">
            <template slot-scope="scope">{{ scope.row.sid_cn }}</template>
          </el-table-column>

          <el-table-column label="是否使用" min-width="50">
            <template slot-scope="scope">{{ scope.row.is_use_cn }}</template>
          </el-table-column>

          <el-table-column label="操作" align="center" min-width="100">
            <template slot-scope="scope">
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                type="primary"
                @click="showEditDialog(scope.row)"
              >查看</el-button>
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                @click="deleteNew(scope.$index, scope.row.id)"
                type="danger"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template v-slot:footerLeft>
        <a class="aBtn aBtn_1 aBtn_lan2" @click="delSelection">删除</a>
      </template>
    </tableComponents>

    <el-dialog
      :close-on-click-modal="false"
      :title="'红包详情'"
      :visible.sync="dioForm"
      class="tl size_13"
      width="800px"
    >
      <el-form label-position="right" label-width="120px" :model="form">
        <!-- <el-form-item label="红包名称">
          <el-col :span="12">
            <el-input size="small" :disabled="end" clearable v-model="form.desc"></el-input>
          </el-col>
        </el-form-item>-->
        <template v-if="end">
          <el-form-item label="额度">
            <el-col :span="12">
              <el-input  :disabled="true" clearable :v-model="form.money | keepTwoNum"></el-input>
            </el-col>
          </el-form-item>

          <el-form-item label="消费满">
            <el-col :span="12">
              <el-input  :disabled="true" clearable v-model="form.xfm"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="指定商品">
            <el-col :span="12">
              <el-input  :disabled="true" clearable v-model="form.pid"></el-input>
            </el-col>
          </el-form-item>

          <el-form-item label="所属商家">
            <el-col :span="12">
              <el-input  :disabled="true" clearable v-model="form.sid_cn"></el-input>
            </el-col>
          </el-form-item>

          <el-form-item label="是否使用">
            <el-col :span="12">
              <el-input  :disabled="true" clearable v-model="form.is_use_cn"></el-input>
            </el-col>
          </el-form-item>

          <el-form-item label="开始时间">
            <el-col :span="12">
              <el-date-picker
                v-model="form.begin_time"
                type="datetime"
                value-format="timestamp"
                placeholder="选择日期"
                style="width: 100%;"
              ></el-date-picker>
            </el-col>
          </el-form-item>

          <el-form-item label="过期时间">
            <el-col :span="12">
              <el-date-picker
                v-model="form.end_time"
                type="datetime"
                value-format="timestamp"
                placeholder="选择日期"
                style="width: 100%;"
              ></el-date-picker>
            </el-col>
          </el-form-item>
          <el-form-item>
            <el-button :loading="$store.state.isLoading" type="primary" @click="dioForm = false">确 定</el-button>
          </el-form-item>
        </template>
      </el-form>
      <!-- <span slot="footer" class="dialog-footer">
        
      </span> -->
    </el-dialog>
    <el-dialog
      :close-on-click-modal="false"
      :title="'发放红包'"
      :visible.sync="myshow"
      width="800px"
      class="tl size_13"
    >
      <packet-add @sendSunc="sendSunc" />
    </el-dialog>
  </div>
</template>
<script>
import HeadTitle from "@/components/currency/HeadTitle.vue";
import packetAdd from "./packet-grant";
import { packetlsSearchData } from "@/assets/js/searchData/searchData"; //引入搜索的数据
export default {
  data() {
    return {
      userSearchData: packetlsSearchData,
      show: false,
      myshow: false,
      loading: false,
      list: [],
      allPage: 0,
      allNum: 0,
      pageSize: 10,
      currentPage: 1,
      editId: "",
      multipleSelection: "",
      arr: [],
      visible: false,
      clickNum: 1,
      updataIndex: "",
      searchAll: {},
      dioForm: false,
      form: {},
      end: false
    };
  },
  components: {
    HeadTitle,
    packetAdd
  },
  methods: {
    sendSunc() {
      this.myshow = false;
      this.getNewsAll(this.searchAll);
    },
    showAddDialog() {
      this.show = true;
      console.log(this.show);
    },
    getSearch(obj) {
      this.searchAll = obj;
      this.$router.push(this.$route.path + "?page=" + 1);
      this.getNewsAll(obj);
    },
    getNewsAll(data) {
      this.loading = true;
      var params = {};
      data = data || {};
      params = data;
      this.$api.getNews.getCouponList(params).then(res => {
        if (res.data.code == 200) {
          this.list = res.data.result;
        }
        this.loading = false;
      });
    },
    handleSizeChange(val) {
      var params = this.searchAll;
      params.page = "";
      params.page_size = val;
      this.getNewsAll(params);
      this.pageSize = val;
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.$router.push(this.$route.path + "?page=" + val);
    },
    handleSwitch(val, id) {
      //审核
      var params = {};
      params["id"] = id;
      val == true ? (params["is_check"] = 1) : (params["is_check"] = 0);
      this.$api.getUser.msgCheck(params).then(res => {
        // this.getNewsAll();
      });
    },
    handleSelectionChange(val) {
      //多选框赋值
      this.multipleSelection = val;
    },
    delSelection() {
      //删除
      this.$swal({
        title: "您确定要删除所选信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思"
      }).then(willDelete => {
        if (willDelete.value) {
          var str = "";
          for (var i in this.multipleSelection) {
            str += this.multipleSelection[i].id + "@";
          }
          str = str.slice(0, -1);

          var data = { id_str: str };
          this.$api.getNews.CouponDel(data).then(res => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了所选信息。", "success");
              this.getNewsAll();
            } else {
              this.$swal("删除失败！", "出错啦！", "success");
            }
          });
        }
      });
    },
    deleteNew(index, id) {
      this.$swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思"
      }).then(willDelete => {
        if (willDelete.value) {
          var data = { id_str: id };
          this.$api.getNews.CouponDel(data).then(res => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");

              this.getNewsAll();
            } else {
              this.$swal("删除失败！", "出错啦！", "success");
            }
          });
        }
      });
    },
    showEditDialog(row) {
      this.form = row;
      this.dioForm = true;
      this.end = true;
      this.form.begin_time = this.$fnc.vuetime(this.form.begin_time);
      this.form.end_time = this.$fnc.vuetime(this.form.end_time);
    },
    reload(id) {
      this.getNewsAll(this.searchAll);
    }
  },

  created() {
    this.getNewsAll();
  },
  computed: {},
  watch: {
    $route() {
      var params = this.searchAll;
      params.page = this.$route.query.page || 1;
      params.page_size = this.pageSize;
      this.getNewsAll(params);
      this.currentPage = Number(this.$route.query.page) || 1;
    }
  }
};
</script>

<style lang="less" scoped>
thead > tr > th {
  height: 45px;
  line-height: 45px;
}
td,
th {
  border: 1px solid #e1e3e4;
  font-size: 12px;
}
th {
  background: #f8f8f8;
  font-weight: normal;
  color: #323437;
}
td {
  height: 75px;
  color: #000;
}
.caozuo {
  padding-left: 50px;
}
.pl {
  padding-left: 15px;
}
.chkBox {
  display: block;
  margin: 0 auto;
  width: 18px;
  height: 18px;
  background: #fff;
}
.aBtn_1 {
  padding: 10px;
}
</style>
